﻿using System;
using System.Web.Mvc;
using FikriKibar.Business.Abstract;
using FikriKibar.Business.Concrete;
using FikriKibar.Common.Entities;
using FikriKibar.Common.Exceptions;
using FikriKibar.Common.Helpers;
using FikriKibar.Entities.Abstract;
using FikriKibar.Entities.Composite.BaseModels;
using FikriKibar.Entities.Concrete;
using FikriKibar.Globalization;
using FikriKibar.UI.WebCommon.Infrastructure;

namespace FikriKibar.UI.WebManagement.Controllers
{
    public class ContentTypeController : FikriKibarController
    {
        #region Global Declarations
        private readonly ISettingService _settingService = new SettingManager();

        private readonly IContentTypeService _contentTypeService = new ContentTypeManager();

        private readonly ILanguageService _languageService = new LanguageManager();
        #endregion
        #region Methods


        [FikriKibarSecurityFilter(1)]
        public ViewResult Index()
        {
            var sessionListModel = new ListModel<ContentTypeLanguageLine>();
            if (Session["ContentTypeListSession"] != null)
            {
                sessionListModel = (ListModel<ContentTypeLanguageLine>)Session["ContentTypeListSession"];
            }
            else
            {
                sessionListModel.Paging = new Paging
                {
                    CurrentPage = 1,
                    CurrentPageSize = _settingService.GetValueByKey("DefaultPageSize").ConvertToInt()
                };
                sessionListModel.Searched = string.Empty;
                sessionListModel.SelectedLanguageCode = FikriKibarControllerIdentity.LanguageCode;
                sessionListModel.SelectedStatus = -1;
                Session["ContentTypeListSession"] = sessionListModel;
            }

            var model = _contentTypeService.GetList(sessionListModel.Searched, sessionListModel.SelectedLanguageCode, sessionListModel.SelectedStatus, sessionListModel.Paging.CurrentPage, sessionListModel.Paging.CurrentPageSize, null, null, null);


            return View(model);
        }


        [FikriKibarSecurityFilter(1)]
        [HttpPost]
        public ActionResult Index(ListModel<ContentTypeLanguageLine> model)
        {
            Session["ContentTypeListSession"] = new ListModel<ContentTypeLanguageLine>
            {
                Paging = new Paging
                {
                    CurrentPage = model.Paging.CurrentPage,
                    CurrentPageSize = model.Paging.CurrentPageSize
                },
                Searched = model.Searched ?? string.Empty,
                SelectedLanguageCode = model.SelectedLanguageCode,
                SelectedStatus = model.SelectedStatus

            };
            return View(_contentTypeService.GetList(model.Searched ?? string.Empty, model.SelectedLanguageCode, model.SelectedStatus, model.Paging.CurrentPage, model.Paging.CurrentPageSize, model.IdsForSort, model.IdsForApprove, model.IdsForDelete));
        }

        [FikriKibarSecurityFilter(1)]
        public ViewResult Add()
        {
            return View(_contentTypeService.Add());
        }

        [FikriKibarSecurityFilter(1)]
        [HttpPost]
        public ActionResult Add(AddModel<ContentTypeLanguageLine> model)
        {

            try
            {
                _contentTypeService.Add(model);
                return RedirectToAction("Index", "ContentType");
            }
            catch (ValidationException exception)
            {
                var validationResult = exception.ValidationResult;
                foreach (var t in validationResult)
                {
                    ModelState.AddModelError(t.PropertyName, t.ErrorMessage);
                }
            }

            catch (Exception exception)
            {
                model.Message = exception.Message;
            }

            finally
            {
                model.LanguageList = _languageService.GetAll();
            }
            return View(model);

        }

        [FikriKibarSecurityFilter(1)]
        public ActionResult Update(string id, string language)
        {

            try
            {
                return View(_contentTypeService.Update(id.ConvertToInt(), language));
            }
            catch (NotFoundException)
            {
                TempData["RedirectionModel"] = new RedirectionModel
                {
                    Message = Resources.NotFound,
                    CssClass = "alert alert-danger",
                    Timeout = 2,
                    Url = "/" + FikriKibarControllerIdentity.LanguageCode + "/ContentType"
                };

                return RedirectToAction("Redirect", "Redirection");
            }
        }

        [FikriKibarSecurityFilter(1)]
        [HttpPost]
        public ActionResult Update(UpdateModel<ContentTypeLanguageLine> model)
        {



            try
            {
                _contentTypeService.Update(model);

                TempData["RedirectionModel"] = new RedirectionModel
                {
                    Message = Resources.SaveSuccessMessage,
                    CssClass = "alert alert-success",
                    Timeout = 2,
                    Url = "/" + FikriKibarControllerIdentity.LanguageCode + "/ContentType"
                };

                return RedirectToAction("Redirect", "Redirection");


            }
            catch (ValidationException exception)
            {
                var validationResult = exception.ValidationResult;
                foreach (var t in validationResult)
                {
                    ModelState.AddModelError(t.PropertyName, t.ErrorMessage);
                }
            }

            catch (Exception exception)
            {
                model.Message = exception.Message;
            }

            finally
            {
                model.LanguageList = _languageService.GetAll();
            }
            return View(model);
        }

        [FikriKibarSecurityFilter(1)]
        public ActionResult Delete(int id, string language)
        {
            _contentTypeService.Delete(id, language);

            TempData["RedirectionModel"] = new RedirectionModel
            {
                Message = Resources.DeleteSuccessMessage,
                CssClass = "alert alert-success",
                Timeout = 2,
                Url = "/" + FikriKibarControllerIdentity.LanguageCode + "/ContentType"
            };

            return RedirectToAction("Redirect", "Redirection");


        }
        #endregion
    }
}